package com.zte.cl.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import us.codecraft.webmagic.selector.Html;

import com.zte.cl.model.News;
import com.zte.cl.model.OrigWebpage;
import com.zte.cl.repository.OrigWebpageRepository;
import com.zte.cl.service.NewsQueue;
import com.zte.cl.service.NewsService;
import com.zte.cl.service.PageContentProcessService;
import com.zte.cl.utils.UuidUtil;

@Service("pageContentProcessServiceSeedPageImpl")
public class PageContentProcessServiceSeedPageImpl implements
		PageContentProcessService {

	@Resource
	private NewsService newsService;
	
	@Resource
	private OrigWebpageRepository origWebpageRepository;
	
	/**
	 * 种子页内容处理, 不需要处理原始网页origHtml == null
	 */
	@Override
	@Transactional(rollbackFor = {Exception.class})
	public void processContent(News news, Html origHtml) {
		OrigWebpage ow = new OrigWebpage();
		ow.setPageId(UuidUtil.getShortUuid());
		ow.setChannelId(news.getChannelId());
		ow.setUrl(news.getPageUrl());
		
		origWebpageRepository.save(ow);
		
		news.setPageId(ow.getPageId());
		news.setIsEnabled(false);
		newsService.save(news);
		
		// 放入内容页分析队列
		NewsQueue.push(news);
	}

}
